package br.edu.univas.jobmanager.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import br.edu.univas.jobmanager.controller.ControllerRelatorioTarefas;
import br.edu.univas.jobmanager.entity.Tarefa;
import br.edu.univas.jobmanager.listener.FiltroRelatorioListener;

public class PanelHistoricoTarefas extends JPanel {
	private static final long serialVersionUID = 1L;
	
	
	private JTable tarefasTable;
	private JScrollPane scroll;
	private JLabel numeroDeRegistros;
	
	
	private FiltroRelatorioListener _listener;
	private ControllerRelatorioTarefas _controller;
	public PanelHistoricoTarefas(ControllerRelatorioTarefas _controller) {
		this._controller = _controller;
		_listener = _controller.getFiltrosListener();
		setLayout(new BorderLayout());
		initialize();
	}
	
	
	private void initialize() {
		add(getScroll(),BorderLayout.CENTER);
		add(getNumeroDeRegistros(),BorderLayout.SOUTH);
	}


	private JTable getTarefasTable() {
		if(tarefasTable == null){
			tarefasTable = new JTable();
			tarefasTable.setModel(_controller.getTableModel());
			tarefasTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			tarefasTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			tarefasTable.getColumnModel().getColumn(0).setPreferredWidth(25);
			tarefasTable.getColumnModel().getColumn(1).setPreferredWidth(250);
			tarefasTable.getColumnModel().getColumn(2).setPreferredWidth(75);
			tarefasTable.getColumnModel().getColumn(3).setPreferredWidth(75);
			tarefasTable.getColumnModel().getColumn(4).setPreferredWidth(130);
			tarefasTable.getColumnModel().getColumn(5).setPreferredWidth(130);
			tarefasTable.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					Integer linha = tarefasTable.getSelectedRow();
					Tarefa tarefa = _controller.getTableModel().getItem(linha);
					int op =JOptionPane.showConfirmDialog(null, "Deseja apagar a tarefa "+tarefa.getNome());
					if( op == JOptionPane.YES_OPTION){
						_listener.deletarAction(tarefa);
					}
				}
			});
			tarefasTable.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					if(e.getClickCount()==2){
						Integer linha = tarefasTable.getSelectedRow();
						Tarefa tarefa = _controller.getTableModel().getItem(linha);
						_listener.editarAction(tarefa);
					}
				}
			});
			tarefasTable.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					if(e.getClickCount() == 2){
						Integer linha = tarefasTable.getSelectedRow();
						@SuppressWarnings("unused")
						Tarefa tarefa = _controller.getTableModel().getItem(linha);
//						_listener.atualizarAction(tarefa);
					}
				}
			});
		}
		return tarefasTable;
	}
	private JScrollPane getScroll() {
		if(scroll == null){
			scroll = new JScrollPane(getTarefasTable());
			scroll.setSize(new Dimension(800,300));
		}
		return scroll;
	}
	private JLabel getNumeroDeRegistros() {
		if(numeroDeRegistros == null){
			numeroDeRegistros = new JLabel("");
		}
		return numeroDeRegistros;
	}
	
	public void atualizaNumeroDeRegistros(){
		getNumeroDeRegistros().setText("Registros: "+_controller.getTableModel().getRowCount());
	}
	
	public GridBagConstraints createConstraints(int linha, int coluna, int gridWidth,int gridHeight){
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(5, 5, 5, 5);
		gbc.gridx = coluna;
		gbc.gridy = linha;
		gbc.gridwidth = gridWidth;
		gbc.gridheight = gridHeight;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		return gbc;
	}

}
